<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2229 – This does not read `foo`. Remove `$`/`${}` for that, or use `${var?}` to quiet.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2229 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2229">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="this-does-not-read-foo-remove--for-that-or-use-var-to-quiet">This
does not read <code>foo</code>. Remove <code>$</code>/<code>${}</code>
for that, or use <code>${var?}</code> to quiet.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2229.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">read</span> <span class="va">$foo</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2229.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">read</span> <span class="va">foo</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p><code>read</code> takes a variable name, but shellcheck has noticed
that you give it an expanded variable instead. This will populate
whatever the variable expans to instead of the variable itself. For
example:</p>
<pre><code>foo=bar
read $foo   # Reads data into &#39;bar&#39;, not into &#39;foo&#39;
read foo    # Reads data into &#39;foo&#39;</code></pre>
<h3 id="exceptions">Exceptions:</h3>
<p>If this is intentional and you do want to read a variable through an
indirect reference, you can silence this warning with a directive:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="SC2229.html#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># shellcheck disable=SC2229</span></span>
<span id="cb4-2"><a href="SC2229.html#cb4-2" aria-hidden="true" tabindex="-1"></a><span class="bu">read</span> <span class="st">&quot;</span><span class="va">$foo</span><span class="st">&quot;</span></span></code></pre></div>
<p>Or take advantage of the fact that ShellCheck only warns when no
parameter expansion modifiers are applied:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb5-1"><a href="SC2229.html#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="bu">read</span> <span class="st">&quot;</span><span class="va">${foo}</span><span class="st">&quot;</span>    <span class="co"># ShellCheck warns</span></span>
<span id="cb5-2"><a href="SC2229.html#cb5-2" aria-hidden="true" tabindex="-1"></a><span class="bu">read</span> <span class="st">&quot;</span><span class="va">${foo</span><span class="op">?</span><span class="va">}</span><span class="st">&quot;</span>   <span class="co"># No warning</span></span></code></pre></div>
<p><code>${foo?}</code> fails when <code>foo</code> is unset, which is
fine since <code>read</code> would have failed too. The main side effect
is an improved runtime error message in that case.</p>
<h3 id="related-resources">Related resources:</h3>
<ul>
<li><a href="https://mywiki.wooledge.org/BashPitfalls#pf12">Bash
Pitfalls #12</a>: <code>read $foo</code></li>
</ul>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


